package edu.sysu.cshare.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "T_COURSE")
public class Course implements Serializable {

	private long id;
	private String examDesc;
	private short maxCntPerGroup;
	private String name;
	private int points;
	private String type;
	private User teacher;
	private List<Clazz> classes;
	private List<Group> groups;
	private List<Homework> homeworks;
	private List<User> students;

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	@Column(length = 255)
	public String getExamDesc() {
		return examDesc;
	}

	public void setExamDesc(String examDesc) {
		this.examDesc = examDesc;
	}

	public short getMaxCntPerGroup() {
		return maxCntPerGroup;
	}

	public void setMaxCntPerGroup(short maxCntPerGroup) {
		this.maxCntPerGroup = maxCntPerGroup;
	}

	@Column(length = 255)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPoints() {
		return points;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	@Column(length = 255)
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@OneToOne
	public User getTeacher() {
		return teacher;
	}

	public void setTeacher(User teacher) {
		this.teacher = teacher;
	}

	@ManyToMany(targetEntity = Clazz.class, fetch = FetchType.EAGER)
	@JoinTable(name = "TR_COURSE_CLASS", joinColumns = @JoinColumn(name = "COURSE_ID", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "ClAZZ_ID", referencedColumnName = "id"))
	@Fetch(FetchMode.SUBSELECT)
	public List<Clazz> getClasses() {
		return classes;
	}

	public void setClasses(List<Clazz> classes) {
		this.classes = classes;
	}

	@OneToMany(mappedBy = "course", cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	@OneToMany(mappedBy = "course", cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	public List<Homework> getHomeworks() {
		return homeworks;
	}

	public void setHomeworks(List<Homework> homeworks) {
		this.homeworks = homeworks;
	}

	@ManyToMany(mappedBy = "courses", targetEntity = User.class, fetch = FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	public List<User> getStudents() {
		return students;
	}

	public void setStudents(List<User> students) {
		this.students = students;
	}

}
